package com.sc.sysconfig.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.sysconfig.dao.SysMenuMapper;
import com.sc.sysconfig.dto.MenuQueryDTO;
import com.sc.sysconfig.entity.SysMenu;
import com.sc.sysconfig.service.SysMenuService;
import com.sc.sysconfig.vo.CascaderVO;
import com.sc.sysconfig.vo.OptionVO;
import com.sc.sysconfig.vo.PageVO;
import com.sc.sysconfig.vo.TreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 系统菜单服务impl
 *
 * @author 徐赫阳
 * @date 2021/09/02
 */
@Service
@Component
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    /**
     * 系统菜单映射器
     */
    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 把菜单列表
     *
     * @param queryDTO 查询dto
     * @return {@link PageVO}<{@link SysMenu}>
     */
    @Override
    public PageVO<SysMenu> getMenuList(MenuQueryDTO queryDTO){
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SysMenu::getIsDel,0)
                .isNull(SysMenu::getParent)
                .like(queryDTO.getName() != null,SysMenu::getName,queryDTO.getName())
                .like(queryDTO.getUrl() != null,SysMenu::getUrl,queryDTO.getUrl())
                .eq(queryDTO.getStatus()!= null,SysMenu::getStatus,queryDTO.getStatus())
                .orderByAsc(SysMenu::getMenuOrder);
        Integer count = this.sysMenuMapper.selectList(queryWrapper).size();
        List<SysMenu> list = this.sysMenuMapper.selectList(queryWrapper.lambda().last("LIMIT "+ (queryDTO.getPage() - 1) * queryDTO.getPageSize() + ","+ queryDTO.getPageSize()));
        for(SysMenu menu:list){
            QueryWrapper<SysMenu> queryWrapperSub = new QueryWrapper<>();
            queryWrapperSub.lambda()
                    .eq(SysMenu::getIsDel,0)
                    .eq(SysMenu::getType,1)
                    .eq(SysMenu::getParent,menu.getId())
                    .orderByAsc(SysMenu::getMenuOrder);
            menu.setChildren(this.sysMenuMapper.selectList(queryWrapperSub));
        }
        return PageVO.setPage(queryDTO.getPage(),queryDTO.getPageSize(),list,count);
    }

    /**
     * 把菜单列表
     *
     * @param queryDTO 查询dto
     * @return {@link PageVO}<{@link SysMenu}>
     */
    @Override
    public PageVO<SysMenu> getMenuButtonList(MenuQueryDTO queryDTO){
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SysMenu::getIsDel,0)
                .eq(SysMenu::getType,2)
                .eq(queryDTO.getParent() != null,SysMenu::getParent,queryDTO.getParent())
                .orderByAsc(SysMenu::getCreateTime);
        Integer count = this.sysMenuMapper.selectList(queryWrapper).size();
        List<SysMenu> list = this.sysMenuMapper.selectList(queryWrapper.lambda().last("LIMIT "+ (queryDTO.getPage() - 1) * queryDTO.getPageSize() + ","+ queryDTO.getPageSize()));
        return PageVO.setPage(queryDTO.getPage(),queryDTO.getPageSize(),list,count);
    }

    /**
     * 把菜单选项
     *
     * @return {@link List}<{@link OptionVO}>
     */
    @Override
    public List<OptionVO> getMenuOption(){
        return this.sysMenuMapper.selectMenuOption();
    }

    /**
     * 把菜单选项
     *
     * @return {@link List}<{@link OptionVO}>
     */
    @Override
    public List<CascaderVO> getMenuCascader(){
        List<CascaderVO> list = this.sysMenuMapper.selectMenuCascader();
        this.setChild(list);
        return list;
    }

    /**
     * 获取用户菜单
     *
     * @param loginName 登录名
     * @return {@link List}<{@link SysMenu}>
     */
    @Override
    public List<SysMenu> getUserMenu(String loginName){
        List<SysMenu> menus = this.sysMenuMapper.selectUserMenu(loginName);
        this.setUserMenuChild(menus,loginName);
        return menus;
    }



    /**
     * 把菜单树
     *
     * @return {@link List}<{@link TreeVO}>
     */
    @Override
    public List<TreeVO> getMenuTree(){
        List<TreeVO> trees = this.sysMenuMapper.selectMenuTreeTop();
        setTreeChild(trees);
        return trees;
    }

    public void setChild(List<CascaderVO> list){
        if(list != null && !list.isEmpty()){
            for(CascaderVO c:list){
                List<CascaderVO> clist = this.sysMenuMapper.selectMenuCascaderChild(c.getValue());
                setChild(clist);
                c.setChildren(clist);
            }
        }
    }

    public void setTreeChild(List<TreeVO> list){
        if(list != null && !list.isEmpty()){
            for(TreeVO t:list){
                List<TreeVO> tList = this.sysMenuMapper.selectMenuTreeChild(t.getId());
                setTreeChild(tList);
                t.setChildren(tList);
            }
        }
    }

    public void setUserMenuChild(List<SysMenu> list,String loginName){
        if(list != null && !list.isEmpty()){
            for(SysMenu m:list){
                List<SysMenu> clist = this.sysMenuMapper.selectUserMenuSub(m.getId(),loginName);
                setUserMenuChild(clist,loginName);
                m.setChildren(clist);
                List<String> bList = this.sysMenuMapper.selectMenuButtons(m.getId(),loginName);
                if(bList != null && !bList.isEmpty()){
                    m.setButtons(bList.stream().collect(Collectors.joining(",")));
                }
            }
        }
    }

}
